package com.lb.bs.client.processor.handle.dynamics;

import com.lb.bs.client.annotation.ProcessorMark;
import com.lb.bs.client.annotation.SingletonMark;
import com.lb.bs.client.repository.StoreCenter;
import com.lb.bs.client.beans.LBItemBean;
import com.lb.bs.client.factory.SingletonFactory;
import com.lb.bs.client.processor.ProcessorHandleService;
import com.lb.bs.client.processor.ProcessorService;
import com.lb.bs.client.processor.beans.ProcessorDynamicBean;
import com.lb.bs.client.processor.beans.ProcessorDynamicHandlerBean;
import com.lb.bs.client.processor.enums.KeyType;
import com.lb.bs.client.processor.enums.ProcessorType;
import com.lb.bs.client.processor.handle.dynamics.remove.GrayRemoveProcessorHandle;
import com.lb.bs.client.util.GrayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-12-02 15:00
 * description:
 */
@SingletonMark
@ProcessorMark(type = ProcessorType.REMOVE, filterClasses = {GrayRemoveProcessorHandle.class})
public class DynamicRemoveProcessor implements ProcessorService<ProcessorDynamicBean> {
    private static final Logger logger = LoggerFactory.getLogger(DynamicRemoveProcessor.class);

    private static final ProcessorHandleService<ProcessorDynamicHandlerBean> grayRemoveProcessorHandle = SingletonFactory.getInstance(GrayRemoveProcessorHandle.class);

    @Override
    public void process(ProcessorDynamicBean dynamicBean) {
        String zkKey = dynamicBean.getKey();
        logger.info("removeOneItem, key:{}" ,zkKey);

        KeyType keyType = GrayUtil.getKeyType(zkKey);

        StoreCenter storeCenter = StoreCenter.getInstance();
        if (KeyType.ITEM_KEY.equals(keyType)) {
            Map<String, LBItemBean> configItemMap = storeCenter.getItemKeyMap();
            LBItemBean itemBean = configItemMap.get(zkKey);
            if (itemBean == null) {
                return;
            }
            itemBean.setEnable(false);
        }
        if (KeyType.GRAY_ITEM_KEY.equals(keyType) || KeyType.GRAY_IP_KEY.equals(keyType)) {
            ProcessorDynamicHandlerBean handlerBean = new ProcessorDynamicHandlerBean(zkKey, dynamicBean.getData(), false);
            grayRemoveProcessorHandle.handleProcess(handlerBean);
        }
    }
}
